﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using System.IO;
using RuleCode.Table;
using RuleCode.WebControls;

namespace ERPTemplate.Public
{
    public partial class FilterSelect : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request["tablename"] != null && Request["id"] != null)
                {
                    DropDownList1.Items.Clear();
                    string queryxmlpath = RuleCode.WebControls.RuleQueryTable.getXMLFileName(
                        Request["tablename"].ToString(),
                        Request["id"].ToString()
                        );
                    DirectoryInfo dInfo = new DirectoryInfo(queryxmlpath);
                    if (dInfo.Exists == false)
                    {
                        dInfo.Create();
                    }
                    FileInfo[] fInfos = dInfo.GetFiles("*.xml");
                    foreach (FileInfo fInfo in fInfos)
                    {
                        string filename = fInfo.Name.Remove(fInfo.Name.LastIndexOf("."));
                        ListItem li = new ListItem(filename, filename);
                        DropDownList1.Items.Add(li);
                    }
                }
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            if (TextBox1.Text.Trim() != "")
            {
                RuleQueryTable.CreateNewQueryXmlFile(TextBox1.Text.Trim());
                string url = ResolveUrl("~/Public/FilterCustomize.aspx") + "?tablename="
                    + Request["tablename"].ToString() + "&id="
                    + Request["id"].ToString() + "&queryname="
                    + TextBox1.Text.Trim() + "&type=edit"
                    ;
                Response.Redirect(url);
            }
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            if (DropDownList1.SelectedValue != "")
            {
                string url = ResolveUrl("~/Public/FilterCustomize.aspx") + "?tablename="
                    + Request["tablename"].ToString() + "&id="
                    + Request["id"].ToString() + "&queryname="
                    + DropDownList1.SelectedValue + "&type=edit"
                    ;
                Response.Redirect(url);
            }
        }
    }
}
